<template>
    <div class="com_container">
        <div class="select_container" :style="comStyle">
            <div class="select_title">
                <span>{{ curTitle }}</span>
                <span class="title_icon" @click="isShow = !isShow">⇩</span>
            </div>
            <div class="select_option" v-show="isShow" :style="bgcStyle">
                <div class="select_item" v-for="item in selectTypes" :key="item.key" @click="currentSelected(item)">
                    {{item.text}}</div>
            </div>
        </div>
        <!-- 编辑按钮 -->
        <div class="edit">
            <span class="el-icon-edit" @click="dialogVisible = true"></span>
        </div>
        <!-- 编辑框 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form ref="form" label-width="80px">
                <el-form-item label="地区">
                    <el-select placeholder="上海" v-model="curGroup.pro" style="width: 100%;" @change="updateGroup('pro',proValue)">
                        <el-option v-for="item in Options" :key="item.name" :label="item.name"
                                :value="item.name">
                            </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="修改数据">
                    <el-col :span="11">
                        <el-select placeholder="1月" v-model="curGroup.month" style="width: 100%;">
                            <el-option v-for="item in monthOptions" :key="item.label" :label="item.label"
                                :value="item.label">
                            </el-option>
                        </el-select>
                    </el-col>
                    <el-col class="line" :span="2"> -</el-col>
                    <el-col :span="11">
                        <el-input placeholder="销量" v-model="curGroup.sale" style="width: 100%; "></el-input>
                    </el-col>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
            </span>
        </el-dialog>
        <div class="com_chart" ref="trender_ref"></div>
    </div>
</template>

<script>
import { getTheme } from '@/utils/theme_utils'
import { mapState } from 'vuex'


export default {
    // 组件名称
    name: 'trenderChart',
    data() {
        return {
            chartInstance: null,
            allData: null,
            timerId: null,
            isShow: false,
            curType: 'map',
            titleFontSize: 0,
            loading: null,
            dialogVisible: false,
            Options:[],
            monthOptions:[
                {
                    label: '1月',
                },
                {
                    label: '2月',
                },
                {
                    label: '3月',
                },
                {
                    label: '4月',
                },
                {
                    label: '5月',
                },
                {
                    label: '6月',
                },
                {
                    label: '7月',
                },
                {
                    label: '8月',
                },
                {
                    label: '9月',
                },
                {
                    label: '10月',
                },
                {
                    label: '11月',
                },
                {
                    label: '12月',
                }
            ],
            curGroup:{
                pro: '上海',
                month:'1月',
                sale: 155.13
            }
        }
    },
    computed: {
        selectTypes() {
            if (!this.allData) {
                return []
            }
            else {
                let test = this.allData.type.filter(item => {
                    return item.key !== this.curType
                })
                return test
            }
        },
        curTitle() {
            if (!this.allData) {
                return ''
            }
            else {
                return this.allData[this.curType].title
            }
        },
        comStyle() {
            // 特别注意：设置的是fontSize 不是fontsize 也不是font-size
            return {
                fontSize: this.titleFontSize + 'px',
                color: getTheme(this.theme).titleColor
            }
        },
        bgcStyle() {
            return {
                backgroundColor: getTheme(this.theme).backgroundColor
            }
        },
        ...mapState(['theme'])
    },
    watch: {
        theme() {
            this.chartInstance.dispose()
            this.initChart()
            this.adapter()
            this.updateChart()
        }
    },
    created() {
        // 注册回调函数
        this.$scoket.registerCallBack('trendData', this.getData)
    },
    mounted() {
        this.initChart()
        this.loading = this.$loading({
            target: this.$refs.trender_ref,
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        });
        // 获取数据需要使用客户端向服务端发送数据来获取
        this.$scoket.send({
            // 指明此次获取的信息
            action: 'getData',
            socketType: 'trendData',
            chartName: 'trend',
            value: ''
        })
        window.addEventListener('resize', this.adapter)
        // 第一次加载需要手动调用一次
        this.adapter()
    },
    destroyed() {
        window.removeEventListener('resize', this.adapter)
        // 注销回调函数
        this.$scoket.unregisterCallBack('trendData')
    },
    methods: {
        // 初始化echarts实例对象
        initChart() {
            this.chartInstance = this.$echarts.init(this.$refs.trender_ref, this.theme)
            // 对图表初始化配置
            const initOption = {
                // 初始化两轴的类型
                xAxis: {
                    type: 'category',
                    boundaryGap: false
                },
                yAxis: {
                    type: 'value',
                },
                grid: {
                    left: '3%',
                    top: '35%',
                    right: '5%',
                    bottom: '5%',
                    // 坐标轴位置是否包含标签区域
                    containLabel: true
                },
                tooltip: {
                    trigger: 'axis',

                },
                legend: {
                    left: 20,
                    top: '15%',
                    icon: 'circle'
                }
            }
            this.chartInstance.setOption(initOption)
            // 绑定监听事件
            this.chartInstance.on('mouseover', () => {
                clearInterval(this.timerId)
            })
            this.chartInstance.on('mouseout', () => {
                this.startInterval()
            })
        },
        // 获取服务器数据
        getData(ret) {
            // ws调用getData的时候自动传入图标数据当作形参
            this.allData = ret
            this.updateChart()
        },
        // 更新图表
        updateChart() {
            // 半透明的颜色值
            const colorArr1 = [
                'rgba(11, 168, 44, 0.5)',
                'rgba(44, 110, 255, 0.5)',
                'rgba(22, 242, 217, 0.5)',
                'rgba(254, 33, 30, 0.5)',
                'rgba(250, 105, 0, 0.5)'
            ]
            // 全透明的颜色值
            const colorArr2 = [
                'rgba(11, 168, 44, 0)',
                'rgba(44, 110, 255, 0)',
                'rgba(22, 242, 217, 0)',
                'rgba(254, 33, 30, 0)',
                'rgba(250, 105, 0, 0)'
            ]
            const timeArr = this.allData.common.month
            const valueArr = this.allData[this.curType].data
            // 获取y轴每条线的名
            const legendArr = valueArr.map(item => {
                return item.name
            })
            console.log('legendArr', legendArr);
            const seriesArr = valueArr.map((item, index) => {
                return {
                    // 需要给每一条线赋名字，才能绑定legend
                    name: item.name,
                    type: 'line',
                    data: item.data,
                    stack: this.curType,
                    areaStyle: {
                        color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                            {
                                offset: 0,
                                color: colorArr1[index]
                            },
                            {
                                offset: 1,
                                color: colorArr2[index]
                            }
                        ])
                    }
                }
            })
            this.Options = seriesArr
            console.log('seriesArr', seriesArr);
            const dataOption = {
                // 设置类目轴数据
                xAxis: {
                    data: timeArr
                },
                // 设置y轴的数据集series
                series: seriesArr,
                // 设置legend
                legend: {
                    data: legendArr
                }
            }
            if (this.loading) {
                this.loading.close()
            }
            this.chartInstance.setOption(dataOption)
        },
        // 自适应配置更新
        adapter() {
            console.log('adapter调用');
            this.titleFontSize = document.body.clientWidth / 100 * 1.15
            console.log('宽度', this.titleFontSize);
            const adapterOption = {
                legend: {
                    itemWidth: this.titleFontSize,
                    itemHeigth: this.titleFontSize,
                    itemGap: this.titleFontSize,
                    fontSize: this.titleFontSize / 2
                }
            }
            this.chartInstance.setOption(adapterOption)
            this.chartInstance.resize()
        },
        currentSelected(curItem) {
            this.isShow = !this.isShow
            this.curType = curItem.key
            this.updateChart()
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(() => {
                    done();
                })
                .catch(() => { });
        },
        updateGroup(type,arg ){
            if(type == 'pro'){
                console.log(arg);
            }
        }
    }
}
</script> 

<style scoped lang='less'>
.select_container {
    position: absolute;
    top: 10px;
    left: 25px;
    z-index: 10;
    font-weight: 700;
    font-family: sans-serif;

    .title_icon {
        margin-left: 10px;
        cursor: pointer;
    }

    .select_option {
        background-color: #222327;
        text-align: center;

        .select_item {
            cursor: pointer;
        }
    }
}

.edit {
    position: absolute;
    width: 20px;
    height: 20px;
    right: 60px;
    top: 15px;
    cursor: pointer;
    z-index: 9;
    color: white;
}
</style>
